package cc.rengu.igas.channel.wechat.core.service.trans;

import cc.rengu.igas.channel.wechat.common.constant.WechatParamConstant;
import cc.rengu.igas.channel.wechat.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.wechat.core.model.request.NuccWechatPayNoticeRequest;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.NonFinanceTxn;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * 微信支付结果通知
 * Created by 王成 on 2018/6/29.
 */
public class NuccWechatPayNoticeService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.FRONT_SEQ_NUM, xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRACE_NO));
        NuccWechatPayNoticeRequest nuccWechatPayNoticeRequest = new NuccWechatPayNoticeRequest();
        ConvertUtil.convertOutput(nuccWechatPayNoticeRequest);
        return nuccWechatPayNoticeRequest;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        int iReturnCode;
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        NuccWechatPayNoticeRequest wechatPayNoticeRequest = (NuccWechatPayNoticeRequest) request;
        /* 从内部树中获取登记流水信息，用于后续更新转换 */
        NonFinanceTxn nonFinanceTxn = new NonFinanceTxn();
        ConvertUtil.convertOutput(nonFinanceTxn);
        /* 获取支付订单信息 */
        /*OrderInfoMapper orderInfoMapper = new OrderInfoMapperImpl();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        OrderInfo orderInfo = orderInfoMapper.selectIgasOrderInfoByPayId(instId,wechatPayNoticeRequest.getMchntOrderId());
        if (null == orderInfo){
            rglog.error("获取支付订单信息失败,outTradeNo:<{}>,mchntNo:<{}>",wechatPayNoticeRequest.getMchntOrderId(),wechatPayNoticeRequest.getMchntNo());
            throw new BizException(RespCodeEnum.GET_PAY_ORDER_INFO_ERROR.getRespCode(),RespCodeEnum.GET_PAY_ORDER_INFO_ERROR.getRespDesc());
        }
        *//* 检查订单状态 *//*
        if (OrderStatusEnum.PAY_CANCLE.getStatus().equals(orderInfo.getOrderStatus()) ||
                OrderStatusEnum.PAY_REFUND.getStatus().equals(orderInfo.getOrderStatus()) ||
                OrderStatusEnum.CLOSED.getStatus().equals(orderInfo.getOrderStatus())){
            rglog.error("当前订单状态<{}>不允许操作!",orderInfo.getOrderStatus());
            return new BizResponse(AppParamConstant.SYS_ID,RespCodeEnum.TRANS_SUCCESS.getRespCode(),RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        iReturnCode = orderInfoMapper.updateIgasOrderStatusAndPayId(instId,orderInfo.getOrderId(), OrderStatusEnum.PAY_SUCCESS.getStatus(), PayStatusEnum.SUCCESS.getStatus(),orderInfo.getPayId());
        if (0 != iReturnCode){
            rglog.error("更新支付订单状态失败,orderId:<{}>,OrderStatus:<{}>,payStatus:<{}>",orderInfo.getOrderId(),OrderStatusEnum.PAY_SUCCESS.getStatus(),PayStatusEnum.SUCCESS.getStatus());
            throw new BizException(RespCodeEnum.UPDATE_ORDER_STATUS_ERROR.getRespCode(),RespCodeEnum.UPDATE_ORDER_STATUS_ERROR.getRespDesc());
        }
        *//* 查找更新原交易信息 *//*
        FinanceTxnMapper txnMapper = new FinanceTxnMapperImpl();
        FinanceTxn orgFinanceTxn = txnMapper.selectFinanceTxnInfoByPrimaryKey(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID),orderInfo.getPayId());
        if (null == orgFinanceTxn){
            rglog.error("根据SYS_SEQ_NUM<{}>及索引号<{}>查询原交易失败!",orderInfo.getPayId(),0);
            throw new BizException(RespCodeEnum.ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.ORIG_TXN_NOT_FOUND.getRespDesc());
        }*/
//        orgFinanceTxn.setPayChannelSeqNum(wechatPayNoticeRequest.getTraceNo());
//        orgFinanceTxn.setPayAcctIssCode(wechatPayNoticeRequest.getPayBankType());
        /* 更新原交易授权状态，表示已支付，供批量使用 */
//        orgFinanceTxn.setBizStatus(BizStatusEnum.PAY_SUCCESS.getStatus());
       /* iReturnCode = txnMapper.updateFinanceTxnByPrimaryKey(orgFinanceTxn);
        if (0 != iReturnCode){
            throw new BizException(RespCodeEnum.ALIP_NOTICE_TXN_ERROR.getRespCode(),RespCodeEnum.ALIP_NOTICE_TXN_ERROR.getRespDesc());
        }*/

        /* 查找更新原交易信息扩展表*/
//        TxnExpandMapper txnExpandMapper = new TxnExpandMapperImpl();
//        TxnExpand txnExpand = txnExpandMapper.selectIgasTxnExpandByPrimaryKey(orgFinanceTxn.getSysSeqNum(), orgFinanceTxn.getTraceIndex());
//        if (null == txnExpand){
//            rglog.error("获取交易扩展流水信息失败,sysSeqNum:<{}>,traceIndex:<{}>",orgFinanceTxn.getSysSeqNum(),orgFinanceTxn.getTraceIndex());
//            throw new BizException(RespCodeEnum.ALIP_NOTICE_TXN_ERROR.getRespCode(),RespCodeEnum.ALIP_NOTICE_TXN_ERROR.getRespDesc());
//        }
//        txnExpand.setRspBuf(JSON.toJSONString(wechatPayNoticeRequest));
//        txnExpand.setAppId(wechatPayNoticeRequest.getAppId());
//        iReturnCode = txnExpandMapper.updateIgasTxnExpandByPrimaryKey(txnExpand);
//        if (0 != iReturnCode){
//            rglog.error("原交易扩展信息更新失败,sysSeqNum:<{}>,traceIndex:<{}>",txnExpand.getSysSeqNum(),orgFinanceTxn.getTraceIndex());
//            throw new BizException(RespCodeEnum.ALIP_NOTICE_TXN_ERROR.getRespCode(),RespCodeEnum.ALIP_NOTICE_TXN_ERROR.getRespDesc());
//        }

        /* 调用商户交易推送通知服务 */
       /* Map<String,String> pushMsgContent = new HashMap<>();
        pushMsgContent.put(IgasTreeNodeConstant.ORDER_ID,orderInfo.getOrderId());
        pushMsgContent.put(IgasTreeNodeConstant.PAY_ORDER_ID,orderInfo.getPayId());
        pushMsgContent.put(IgasTreeNodeConstant.MCHNT_NO,orderInfo.getMchntNo());
        pushMsgContent.put(IgasTreeNodeConstant.PAY_TYPE,orderInfo.getPayType());
        pushMsgContent.put(IgasTreeNodeConstant.TRANS_TIME,DateUtil.getCurrentDateTime("yyyyMMddHHmmss"));
        pushMsgContent.put(IgasTreeNodeConstant.TRANS_CCY,orderInfo.getOrderCcy());
        pushMsgContent.put(IgasTreeNodeConstant.TRANS_AMT,String.valueOf(orderInfo.getOrderAmt()));
        pushMsgContent.put(IgasTreeNodeConstant.PAY_STATUS,PayStatusEnum.SUCCESS.getStatus());
        pushMsgContent.put("txnDesc","微信收款");
        MsgPushRequest msgPushRequest = new MsgPushRequest();
        msgPushRequest.setTxnNum(IgasTxnNumConstant.T00BS900);
        msgPushRequest.setContent(JSON.toJSONString(pushMsgContent));
        ConvertUtil.convertInput(msgPushRequest);
        asynnotify.transactionNotice(IgasServiceConstant.MCHNT_TRADE_RESULT_PUSH_SERVICE, 1, 0, 3 * 1000 * 60);
        //还原交易码
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM,wechatPayNoticeRequest.getTxnNum());

        //商户每日限额统计调用
        TxnLimitStatisService txnLimitStatisService = new TxnLimitStatisServiceImpl();
        TxnLimitStatisBean txnLimitStatisBean = new TxnLimitStatisBean();
        txnLimitStatisBean.setInstId(instId);
        txnLimitStatisBean.setLimitType(TxnLimitTypeEnum.LIMIT_MCH.getLimitType());
        txnLimitStatisBean.setStatisKey(orderInfo.getMchntNo());
        txnLimitStatisBean.setAcctType(TxnLimitAcctTypeEnum.LIMIT_ACCT_ALL.getLimitAcctType());
        txnLimitStatisBean.setLimitMode(TxnLimitModeEnum.LIMIT_AMT_DAY_TXN.getLimitAmtType());
        txnLimitStatisBean.setTransAmt(xmlTreeUtil.getXmlTreeLongValue(IgasTreeNodeConstant.TRANS_AMT));
        txnLimitStatisBean.setMchntNo(orderInfo.getMchntNo());
        txnLimitStatisBean.setTermNo(orderInfo.getRemark2());
        iReturnCode = txnLimitStatisService.UpdatetxnLimitStatis(txnLimitStatisBean);
        if (0 != iReturnCode){
            rglog.error("交易限额统计失败，error,不影响正常交易逻辑");
        }*/

        /* 查找BIZ_PROCESS的注解，基于注解实现不同场景的业务逻辑 */
        invokeBizAnnoMethod(BIZ_PROCESS.class);

        return new BizResponse(WechatParamConstant.IGAS_SYS_ID, RespCodeEnum.TRANS_SUCCESS.getRespCode(), RespCodeEnum.TRANS_SUCCESS.getRespDesc());
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {

    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }
}
